# -*- coding: utf-8 -*-
'''
    User query processor
'''

import logging
import onlinebrary.lib.identifier as identifier

from onlinebrary.lib.discoverer.feed_discoverer import FeedDiscoverer
from onlinebrary.lib.discoverer.html_discoverer import HtmlDiscoverer
from onlinebrary.lib.aggregator.master import AggregatorMaster

log = logging.getLogger(__name__)

from pylons import g

class Processor(object):

    def __init__(self):
        self.agents = {'html' : HtmlDiscoverer(),
                       'feed' : FeedDiscoverer(),
                       'title' : AggregatorMaster()}
        log.debug('Processor created')
        
    def process(self, query):
        log.debug('Query to process: %s', query)
        
        query_type = identifier.identify(query)
            
        block = None
        
        if not query_type in self.agents.keys():
            log.error('No discoverer for content type %s', query_type)
            return g.viewer.parse_error('Неизвестный тип данных %s' % str(query_type))
        
        data = self.agents[query_type].get_info(query)
        
        log.debug('Parsing data')
        return g.viewer.parse(data)
        
        
        
